<template>
  <div>
      <HomeHeader></HomeHeader>
      <HomeSwiper :swiperList="swiperList"></HomeSwiper>
      <HomeIcons :iconList="iconList"></HomeIcons>
      <HomeRecommend :recommendList="recommendList"></HomeRecommend>
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import axios from 'axios'
import {mapState} from 'vuex'
export default {
  name:"Home",
  components:{ 
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend
  },
  data(){
    return{
      lastCity:'',
      swiperList:[],
      iconList:[],
      recommendList:[]
    }
  },
  computed:{
    ...mapState(['city'])
  },
  mounted(){
    this.lastCity=this.city
    this.getHomeInfo()
  },
  methods:{
    getHomeInfo(){
      axios.get('/api/index.json?city='+this.city).then(this.getHomeInfoSuccess)
    },
    getHomeInfoSuccess(res){
      res = res.data
      if(res.ret && res.data){
        console.log(res)
        const data = res.data;
        this.swiperList= data.swiperList;
        this.iconList=data.iconList;
        this.recommendList=data.recommendList
      }
    }
  },
  //keep-live之后有的声明周期函数,防止缓存
  activated(){
    if(this.lastCity!==this.city){
      this.lastCity = this.city
      this.getHomeInfo()
    }
  }
}
</script>

<style>

</style>